const {sendPhoneCode,randomCode} = require("@/zy-common/config/phoneCode")
const {send} =require("@/zy-common/utils")

const sendCode = async (ctx,next) => {
  const {phone} = ctx.request.body
  const code = randomCode()
  try {
    //调用阿里云短信API
    let res = await sendPhoneCode(phone,code)
    if(res.Code === "OK"){
      ctx.user = {phone,code}
      await next()
    }
    //测试
    // ctx.user = {phone,code}
    // await next()
  } catch (err) {
    if(err.code === 'isv.BUSINESS_LIMIT_CONTROL') return send(ctx,400,'-1','验证码频繁触发');
    send(ctx,400,'-1','请重新发送验证码')
    console.log("err",error);
  }
}

module.exports = {
  sendCode
}